package com.example.wastes.common.config;


import com.example.wastes.common.interceptors.AuthorizationInterceptor;
import com.example.wastes.common.interceptors.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyBannerWebAppConfigurer implements WebMvcConfigurer {

    @Autowired
    private AuthorizationInterceptor authorizationInterceptor;

    @Value("${upload.upload-dir-windows}")
    private String windowsFilepathPre;

    @Value("${upload.upload-choose}")
    private int choose;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (choose == 1){
            String windowsFilepath = windowsFilepathPre.replace('\\', '/');
            //win
            registry.addResourceHandler("/**")
                    .addResourceLocations("file:"+windowsFilepath+"/")
                    .addResourceLocations("classpath:/static/");
        } else { //linux
            registry.addResourceHandler("/**")
                    .addResourceLocations("classpath:/static/");
        }

    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        super.addInterceptors(registry);
//        // 拦截器不能拦截/login.html,/loginDao,以及静态资源
        registry.addInterceptor(authorizationInterceptor).addPathPatterns("/user/**")
                .excludePathPatterns("/user/toLogin/**","/","/css/**","/js/**","/img/**","/movie/saveMovie/**","/static/**");
    }
}
